var app = getApp()
Page({
  data: {
    theme: 'light',
    tempFilePath: '',
    savedFilePath: '',
    fileName: '',
    tips: '',
    translateStatus: 0, //0-未开始 1-转换中，2-转换完成
    wordFileName:'',
    wordFilePath:'',
  },
  onLoad() {
    this.setData({
      theme: wx.getSystemInfoSync().theme || 'light'
    })

    if (wx.onThemeChange) {
      wx.onThemeChange(({theme}) => {
        this.setData({theme})
      })
    }
    this.setData({
      savedFilePath: wx.getStorageSync('savedFilePath')
    })
  },

  onShareAppMessage() {
    return {
      title: '文件',
      path: 'packageAPI/pages/media/file/file'
    }
  },
  
  chooseMessageFile() {
    const that = this
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success(res) {
        console.log(res)
        var fileName = res.tempFiles[0].name
        if (fileName.indexOf('pdf') === -1) {
          wx.showToast({
            icon: 'error',
            title: '只支持pdf文件',
          })
          return
        }
        that.setData({
          tempFilePath: res.tempFiles[0].path,
          fileName: fileName
        })
        console.log('文件路径' +that.data.tempFilePath)
        // that.uploadFileToServer(that.data.tempFilePath)
      }
    })
  },
  uploadFileToServer() {
    var token = wx.getStorageSync('token')
    console.log(token)
    this.data.translateStatus = 1
    var that = this
    wx.showLoading({
      title: '转换中...',
    })
    wx.uploadFile({
      url: app.globalData.baseUrl + '/file/pdfToWord', //仅为示例，非真实的接口地址
      filePath: this.data.tempFilePath,
      header: {
        'token':  token// 默认值
      },
      name: 'uploadFile',
      formData: {
        'fileName': this.data.fileName
      },
      success (res){
        console.log(res)
        const data = res.data
        console.log(data)
        var jsonObj = JSON.parse(data)
        if (jsonObj.code === 0) {
          that.setData({
            translateStatus: 2,
            wordFilePath: jsonObj.data.wordFileUrl,
            wordFileName: jsonObj.data.wordFileName
          })
        }else {
          wx.showToast({
            title: jsonObj.message,
          })
        }
        //do something
      },
      fail (res) {
        console.log(res)
      },
      complete(res) {
        wx.hideLoading()
      }
    })
  },

  /**
   * 下载并保存文件
   */
  downloadFileOpen(){
    let link = this.data.wordFilePath;
    console.log(link)
    //下载文件
    wx.downloadFile({
      url: link,
      success (res) {
        console.log(res)
        if (res.statusCode === 200) {
        const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            showMenu:true, //关键点
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },

  saveFile() {
    console.log('文件路径' +this.data.tempFilePath)
    if (this.data.tempFilePath.length > 0) {
      const that = this
      wx.saveFile({
        tempFilePath: this.data.tempFilePath,
        success(res) {
          that.setData({
            savedFilePath: res.savedFilePath
          })
          wx.setStorageSync('savedFilePath', res.savedFilePath)
          wx.showModal({
            title: '保存成功',
            content: '下次进入应用时，此文件仍可用',
          })
        },
        fail() {
          wx.showModal({
            title: '保存失败',
            content: '应该是有 bug 吧',
          })
        }
      })
    }
  },
  clear() {
    wx.setStorageSync('savedFilePath', '')
    this.setData({
      tempFilePath: '',
      savedFilePath: '',
      translateStatus: 0
    })
  },
  navigateToRecord() {
    wx.navigateTo({
      url: '../record/record',
    })
  },
  onLoad() {
    this.setData({
      theme: wx.getSystemInfoSync().theme || 'light'
    })

    if (wx.onThemeChange) {
      wx.onThemeChange(({theme}) => {
        this.setData({theme})
      })
    }
  }
})
